import { getList, deleteList, getModelList, addModel, viewModel, editModel, deleteModel } from '@/api/innerProject/spectrumScan'

const state = {
  count: +'',
  tableData: []
}

const mutations = {
  // 是否展示更新盒子
  update_list (state, data) {
    state.tableData = data
  },
  list_count (state, count) {
    state.count = count
  }

}

const actions = {
  // 刷新列表
  updateTableList (context, payload) {
    payload.projectId = localStorage.getItem('projectId')
    return new Promise((resolve, reject) => {
      getList(payload).then(res => {
        if (res.data.code === (200 || '200') && res.data.data.data.length) {
          context.commit('update_list', res.data.data.data)
          context.commit('list_count', res.data.data.count)
        } else {
          context.commit('update_list', [])
          context.commit('list_count', 0)
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  deleteItem (context, payload) {
    return new Promise((resolve, reject) => {
      deleteList(payload).then(res => {
        if (res.data.code === (200 || '200')) {
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  // model
  getModelData (context, payload) {
    payload.projectId = localStorage.getItem('projectId')
    return new Promise((resolve, reject) => {
      getModelList(payload).then(res => {
        if (res.data.code === (200 || '200') && res.data.data.list.length) {
          // console.log(res.data.data.list)
          context.commit('update_list', res.data.data.list)
          context.commit('list_count', res.data.data.total)
        } else {
          context.commit('update_list', [])
          context.commit('list_count', 0)
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  addModelData (context, payload) {
    payload.projectId = localStorage.getItem('projectId')
    return new Promise((resolve, reject) => {
      addModel(payload).then(res => {
        if (res.data.code === (200 || '200')) {
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  viewModelData (context, payload) {
    return new Promise((resolve, reject) => {
      viewModel(payload).then(res => {
        if (res.data.code === (200 || '200')) {
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  editModelData (context, payload) {
    return new Promise((resolve, reject) => {
      editModel(payload).then(res => {
        if (res.data.code === (200 || '200')) {
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  removeModelData (context, payload) {
    return new Promise((resolve, reject) => {
      deleteModel(payload).then(res => {
        if (res.data.code === (200 || '200')) {
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  }
}
export default {
  namespaced: true,
  actions,
  state,
  mutations
}
